<?php

namespace ApiBundle\Controller\V1;

use ApiBundle\Controller\BaseController;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\View\View;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;

/**
 * @author  coffey  <coffey@nligo.com>
 *
 * 下单操作接口类
 * Class OrderController
 * @package ApiBundle\Controller\V1
 */
class OrderController extends BaseController
{
    /**
     * @author  coffey
     * 用户下注
     *
     * @ApiDoc(
     *  resource=true,
     *  description="用户下注",
     *  parameters={
     *      {"name"="userToken", "dataType"="string","required"=true,"description"="用户token"},
     *      {"name"="betNum", "dataType"="string","required"=true,"description"="下注号码 /123/23/12这种格式"},
     *      {"name"="nperId", "dataType"="string","required"=true,"description"="期号ID"},
     *  },
     * )
     * @Rest\POST("/user/bet")
     */
    public function userBetAction(Request $request)
    {
        $param['userToken'] = $request->request->get('userToken','');
        $param['nperId'] = $request->request->get('nperId','');
        $param['betNum'] = $request->request->get('betNum','0/0/0');
        if($this->checkParam($param) == false)
        {
            return $this->FinalJson(1);
        }
        $tokenData = $this->checkToken($param['userToken']);
        if(isset($tokenData['code']) && $tokenData['code'] == 1)
        {
            $userInfo = $tokenData['userinfo'] ? $tokenData['userinfo'] : array();
            if(!isset($userInfo['userId']) || empty($userInfo['userId']))
            {
                return $this->FinalJson(5,array());
            }
            $userId = $userInfo['userId'];
        }
        else
        {
            return $this->FinalJson($tokenData['errcode']);
        }
        $nperInfo = $this->getGamesNperRecordRepository()->findOneBy(array('nper'=>$param['nperId']));
        if(empty($nperInfo))
        {
            return $this->FinalJson(1);
        }
        $handleData = $this->container->get('games_handle_order_data');
        $result = $handleData->createData($param['betNum'],$nperInfo);
        return  $this->FinalJson($result['code'],$result['data']);
    }

    /**
     * @author  coffey
     * 取消下注
     *
     * @ApiDoc(
     *  resource=false,
     *  description="用户取消下注",
     *  parameters={
     *      {"name"="userToken", "dataType"="string","required"=true,"description"="用户token"},
     *      {"name"="nperId", "dataType"="string","required"=true,"description"="本期号码"},
     *  },
     * )
     * @Rest\POST("/user/cancelbet")
     */
    public function cancelbetAction(Request $request)
    {
        $param['nperId'] = $request->request->getInt('nperId');
        $param['userToken'] = $request->request->get('userToken','');
        if($this->checkParam($param) == false)
        {
            return $this->FinalJson(1);
        }
        $tokenData = $this->checkToken($param['userToken']);
        if(isset($tokenData['code']) && $tokenData['code'] == 1)
        {
            $userInfo = $tokenData['userinfo'] ? $tokenData['userinfo'] : array();
            if(!isset($userInfo['userId']) || empty($userInfo['userId']))
            {
                return $this->FinalJson(5,array());
            }
            $userId =   $userInfo['userId'];
        }
        else
        {
            return $this->FinalJson($tokenData['errcode']);
        }
        $nperInfo = $this->getGamesNperRecordRepository()->findOneBy(array('nper'=>$param['nperId']));
        if(empty($nperInfo))
        {
            return $this->FinalJson(1);
        }
        $handleData = $this->container->get('games_handle_order_data');
        $result = $handleData->cancelOrder($userId,$param['nperId']);
        return  $this->FinalJson($result['code'],$result['data']);
    }

    /**
     * @author  coffey
     * 开奖接口
     *
     * @ApiDoc(
     *  resource=false,
     *  description="公布开奖",
     *  parameters={
     *      {"name"="nperId", "dataType"="string","required"=true,"description"="本期号码"},
     *  },
     * )
     * @Rest\POST("/openlottery")
     */
    public function openLotteryAction(Request $request)
    {
        $param['nperId'] = $request->request->getInt('nperId');
        if($this->checkParam($param) == false)
        {
            return $this->FinalJson(1);
        }
    }
}
